home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 60.zip / BS1 part 60 / C mon v3 d3.adf / CMANV3_3.LHA / ACE9.lha / Devices / TimerDevice / Example2.c < prev    next >
C/C++ Source or Header  |  1992-04-27  |  6KB  |  197 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Devices                 Amiga C Club       */
  7. /* Chapter: Timer Device                Tulevagen 22       */
  8. /* File:    Example2.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-27                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20.  
  21.  
  22. /* This example demonstrates how you can send several */
  23. /* requests to the Timer Device.                      */
  24.  
  25.  
  26.  
  27. #include <exec/types.h>    /* STRPTR         */
  28. #include <exec/ports.h>    /* struct Message */
  29. #include <exec/memory.h>   /* MEMF_PUBLIC    */
  30. #include <devices/timer.h> /* TIMERNAME */
  31.  
  32.  
  33.  
  34. /* The reply port: */
  35. struct MsgPort *replymp;
  36.  
  37. /* Pointer to the message: */
  38. struct Message *msg;
  39.  
  40. /* The timer request blocks: */
  41. struct timerequest *tr1;
  42. struct timerequest *tr2;
  43. struct timerequest *tr3;
  44.  
  45. /* When the Timer Device is open this variable is TRUE: */
  46. BOOL not1_opened = TRUE;
  47. BOOL not2_opened = TRUE;
  48. BOOL not3_opened = TRUE;
  49.  
  50.  
  51.  
  52. /* Declare our functions: */
  53. void clean_up();
  54. void main();
  55.  
  56.  
  57.  
  58. void main()
  59. {
  60.   /* Counter: */
  61.   int count;
  62.  
  63.  
  64.   /* Get a reply port: */
  65.   replymp = (struct MsgPort *)
  66.     CreatePort( NULL, 0 );
  67.   if( !replymp )
  68.     clean_up( "Could not create the reply port!" );
  69.  
  70.  
  71.   /* Get three extended request blocks: */
  72.   /* Requestblock 1: */
  73.   tr1 = (struct timerequest *)
  74.     CreateExtIO( replymp, sizeof(struct timerequest) );
  75.   if( !tr1 )
  76.     clean_up( "Could not create requestblock 1!" );
  77.  
  78.  
  79.   /* Requestblock 2: */
  80.   tr2 = (struct timerequest *)
  81.     CreateExtIO( replymp, sizeof(struct timerequest) );
  82.   if( !tr2 )
  83.     clean_up( "Could not create requestblock 2!" );
  84.  
  85.   /* Requestblock 3: */
  86.   tr3 = (struct timerequest *)
  87.     CreateExtIO( replymp, sizeof(struct timerequest) );
  88.   if( !tr3 )
  89.     clean_up( "Could not create requestblock 3!" );
  90.  
  91.  
  92.  
  93.   /* Since each request block must be linked together with the  */
  94.   /* timer device we have to open the timer device three times. */
  95.   /* We could of course open the timer device once, and then    */
  96.   /* copy the first request block to the other two blocks. If   */
  97.   /* the device can be used exclusively this is then the only   */
  98.   /* solution (exclusive mode means that only one user may use  */
  99.   /* the device). For example, see chapter 27 "Serial Device".  */
  100.   /* However, since the timer device does not have any limit    */
  101.   /* of number of users, we demonstrate this and open it three  */
  102.   /* times.                                                     */
  103.  
  104.  
  105.  
  106.   /* Open the Timer Device: */
  107.   not1_opened = OpenDevice( TIMERNAME, UNIT_VBLANK, tr1 ,0 );
  108.   if( not1_opened )
  109.     clean_up( "Could not open the Timer Device (1)!" );
  110.  
  111.   not2_opened = OpenDevice( TIMERNAME, UNIT_VBLANK, tr2 ,0 );
  112.   if( not2_opened )
  113.     clean_up( "Could not open the Timer Device (2)!" );
  114.  
  115.   not3_opened = OpenDevice( TIMERNAME, UNIT_VBLANK, tr3 ,0 );
  116.   if( not3_opened )
  117.     clean_up( "Could not open the Timer Device (3)!" );
  118.  
  119.  
  120.  
  121.   /* Set our request: */
  122.   /* Requestblock 1: */
  123.   tr1->tr_node.io_Command = TR_ADDREQUEST; /* Add a request.   */
  124.   tr1->tr_time.tv_secs = 10;               /* 10 seconds.      */
  125.   tr1->tr_time.tv_micro = 0;               /* 0 micro seconds. */
  126.  
  127.   /* Requestblock 2: */
  128.   tr2->tr_node.io_Command = TR_ADDREQUEST; /* Add a request.   */
  129.   tr2->tr_time.tv_secs = 7;                /* 7 seconds.       */
  130.   tr2->tr_time.tv_micro = 0;               /* 0 micro seconds. */
  131.  
  132.   /* Requestblock 3: */
  133.   tr3->tr_node.io_Command = TR_ADDREQUEST; /* Add a request.   */
  134.   tr3->tr_time.tv_secs = 5;                /* 5 seconds.       */
  135.   tr3->tr_time.tv_micro = 0;               /* 0 micro seconds. */
  136.  
  137.  
  138.  
  139.   /* Do our requests: (Do not wait) */
  140.   SendIO( tr1 );
  141.   SendIO( tr2 );
  142.   SendIO( tr3 );
  143.  
  144.  
  145.  
  146.   count = 0;
  147.   while( count < 3 )
  148.   {
  149.     /* Add counter: */
  150.     count++;
  151.  
  152.     /* Wait for a message to arrive: */
  153.     msg = (struct Message *) WaitPort( replymp );
  154.  
  155.     /* One of our requests has been completed: */   
  156.     printf( "Request done!\n" );
  157.  
  158.     /* Romove the message: */
  159.     msg = (struct Message *) GetMsg( replymp );
  160.   }
  161.  
  162.  
  163.  
  164.   /* Clean up and quit: */
  165.   clean_up( "The End!" );
  166. }
  167.  
  168.  
  169.  
  170. void clean_up( text )
  171. STRPTR text;
  172. {
  173.   /* Close the Timer Device: */
  174.   if( !not1_opened )
  175.     CloseDevice( tr1 );
  176.   if( !not2_opened )
  177.     CloseDevice( tr2 );
  178.   if( !not3_opened )
  179.     CloseDevice( tr3 );
  180.  
  181.   /* Delete the extended request blocks: */
  182.   if( tr1 ) DeleteExtIO( tr1, sizeof( struct timerequest) );
  183.   if( tr2 ) DeleteExtIO( tr2, sizeof( struct timerequest) );
  184.   if( tr3 ) DeleteExtIO( tr3, sizeof( struct timerequest) );
  185.  
  186.   /* Remove the message port: */
  187.   if( replymp )
  188.     DeletePort( replymp );
  189.  
  190.   /* Print the last message: */
  191.   printf( "%s\n", text );
  192.  
  193.   /* Quit: */
  194.   exit( 0 );
  195. }
  196.  
  197.